/**
 * Created by buzhizhe on 2019/2/10.
 */
var controllerName="/Parenttree";
var VM=new Object();


$(document).ready(function() {
    initDataTable();
    addEvents();
});



//加载事件
function addEvents() {
    /*查询按钮*/
    $("#btnQuery").click(function () {
        $("#pageindex").val(0);
        getListData();
    });
    
    /*添加按钮*/
    $("#btnAdd").click(function () {
        popupEdit(1010);
    });

    //点击关闭层
    $("#btnSubmit").click(function () {
        $(".tip").fadeOut(200);
    });
    $(".btnClose").click(function () {
        $(".tip").fadeOut(200);
    });
    /*修改按钮*/
    $(document).on('click', '.btnEdit', function () {
        var id = $(this)[0].attributes["data-id"].value;
        popupEdit(id);
    })
     /*修改按钮*/
    $(document).on('click', '.btnView', function () {
        var id = $(this)[0].attributes["data-id"].value;
        popupView(id);
    })
   /*删除按钮*/
    $(document).on('click', '.btnDelete', function () {
        var id = $(this)[0].attributes["data-id"].value;
        if (confirm("确定要删除此记录吗？删除后不可恢复！")) {
            doDelete(id);
        }
        return false;
    });
   

}
//初始化VM
function initDataTable() {
    VM = new Vue({
        delimiters: ["[[", "]]"],
        el: '#app',
        data: {listdata: []}
    });
    $("#pageindex").val(0);
    getListData();
    //getMoreData();
   
}

//分页调用
function pagerInit(currentPage, totalRecord, pageSize) {
    $('.pagination').pagination(totalRecord, {
        callback: function (page) {
             getListData(page+1);
        },
        current_page: currentPage - 1,
        display_msg: true,
        items_per_page: pageSize
    });
    setTimeout("trColor()", 200);
}

//ajxa获取列表数据
function getListData(pageindex) {
    if (pageindex == undefined) pageindex = 1;
    $("#pageindex").val(pageindex);
    var url = domain + controllerName + "/index";
    var formData = getFormData($('#formQuery'));
    $.ajax({
        type: "POST",
        url: url,
        async: false,
        dataType: "json",
        data: { "formData": JSON.stringify(formData) },
        error: function (request) {
            ajaxError(request,$(this));
        },
        success: function (result) {
            ajaxSuccess(result, $(this));
            VM.listdata = result.data.dataList;
            pagerInit(result.data.pageIndex, result.data.totalRecords, result.data.pageSize);

       }
    });
}

//打开表单
function popupEdit(id) {
    
    if(id==undefined)id=0;
    var url= domain + controllerName + "/get/"+id
    $.ajax({
        type: "get",
        url: url,
        dataType: "json",
        error: function (request) {
            ajaxError(request, $(this));
        },
        success: function (result) {
            ajaxSuccess(result, $(this));
            var code = result.statecode;
            if (code == 0) {
                var data = result.data;
                //data["showstatus"]=getTitleByValue(arr_items["status"],data["status"]);
                for (var k in data) {
                    //console.log(data[k]);
                    $("input[name='" + k + "']").val(data[k]);
                }
                $("#layer-edit").fadeIn(200);
            } else {
                alert("获取失败：" + result.message);
            }
        }
    }
    );
}
//打开表单
function popupView(id) {
    if(id==undefined)id=0;
    var url= domain + controllerName + "/get/"+id
    $.ajax({
        type: "get",
        url: url,
        dataType: "json",
        error: function (request) {
            ajaxError(request, $(this));
        },
        success: function (result) {
            ajaxSuccess(result, $(this));
            var code = result.statecode;
            if (code == 0) {
                //data["statusname"]=getTitleByValue(arr_items["status"],data["status"]);
                var data = result.data;
                for (var k in data) {
                    //console.log(data[k]);
                    $("span[name='" + k + "']").html(data[k]);
                }
                $("#layer-view").fadeIn(200);
            } else {
                $.alert("获取失败：" + result.message);
            }
        }
    }
    );
}

//查询
function formQuerySubmit() {
    $("#pageindex").val(0);
    getListData();
}

//保存
function formEditSubmit() {
    var url = domain + controllerName + "/update";
    var formData = getFormData($('#formEdit'));
    $.ajax({
        type: "post",
        url: url,
        data: { "formData": JSON.stringify(formData) },
        dataType: "json",
        error: function (request) {
            ajaxError(request, $(this));
        },
        success: function (result) {
            ajaxSuccess(result, $(this));
            var code=result.statecode;
            if(code==0){
                $(".tip").fadeOut(200);
                alert("提交成功");
                getListData();
            }else{
                alert("提交失败："+result.message);
            }
        }
      }
    );

}
//删除
function doDelete(id) {
    var url= domain + controllerName + "/delete/"+id
    $.ajax({
        type: "get",
        url: url,
        dataType: "json",
        error: function (request) {
            ajaxError(request, $(this));
        },
        success: function (result) {
            ajaxSuccess(result, $(this));
            var code = result.statecode;
            if (code == 0) {
                alert("提交成功");
                getListData();
            } else {
                $.alert("提交失败：" + result.message);
            }
        }
    }
    );

}




